package com.hj.controller;

import java.io.IOException;
import java.io.PrintWriter;
import java.util.List;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;






import com.alibaba.fastjson.JSON;
import com.hj.entity.CompanyInfo;
import com.hj.service.CompanyInfoService;
import com.hj.utils.Json;
import com.hj.vo.Companyview;



@Controller					
@RequestMapping("/user")	
public class CompanyInfoControler {
	@Autowired
	private CompanyInfoService companyinfoservice;
	@RequestMapping("/sele")
	@ResponseBody
	public void search(CompanyInfo u,HttpServletRequest req,HttpServletResponse resp){
		resp.setContentType("text/html;charset=UTF-8");
		List<CompanyInfo> us=companyinfoservice.search(u);
		req.setAttribute("cp", us);
		Json j=new Json();	
		if(us!=null&&!us.isEmpty()){
			j.setFlag(true);			
			j.setObj(us);
		}else{
			j.setFlag(false);
			j.setMsg("公司信息不存在!");
		}
		try {
			PrintWriter pw=resp.getWriter();
			String s=JSON.toJSONString(j);
			pw.write(s);
			pw.flush();
			pw.close();			
		} catch (IOException e) {
			e.printStackTrace();
		}		
	}
	@RequestMapping("/selebalance")	
	public void selebalance(HttpServletRequest req,HttpServletResponse resp){
		String name=req.getParameter("name");
		resp.setContentType("text/html;charset=UTF-8");
		List<CompanyInfo> us = null;
		if(name.equals("查询欠费")){
			us=companyinfoservice.selebalance1();
		}
		if(name.equals("查询告警")){
			us=companyinfoservice.selebalance2();
		}
		if(name.equals("查询正常")){
			us=companyinfoservice.selebalance3();
		}
		req.setAttribute("cp", us);
		Json j=new Json();
		if(us!=null&&!us.isEmpty()){
			j.setFlag(true);
			j.setObj(us);
		}else{
			j.setFlag(false);
			j.setMsg("您查询的信息不存在!");
		}
		try {
			PrintWriter pw=resp.getWriter();
			String s=JSON.toJSONString(j);
			pw.write(s);
			pw.flush();
			pw.close();			
		} catch (IOException e) {
			e.printStackTrace();
		}		
	}
	
	@RequestMapping("/{id}")
	public String change(@PathVariable(value="id") String mm,HttpServletRequest req,HttpServletResponse resp){
		resp.setContentType("text/html;charset=UTF-8");
		CompanyInfo us=companyinfoservice.searchone(mm);
		req.setAttribute("s",us);
		return "/form";
	}
	
	@RequestMapping("/change")
	public String change(Companyview uv,HttpServletRequest req,HttpServletResponse resp) throws IOException{
		resp.setContentType("text/html;charset=UTF-8");
		Json j=new Json();
		companyinfoservice.change(uv);
		if(uv.getCompany_name()!=null){
			j.setFlag(true);
			j.setMsg("修改成功!");
		}else{
			j.setFlag(false);
			j.setMsg("修改失败!");
		}
		PrintWriter pw=resp.getWriter();
		String s=JSON.toJSONString(j);
		pw.write(s);
		pw.flush();
		pw.close();
		return "/companyinfo";
		
	}
}
